package cn.rcth.controller;

import cn.rcth.base.VO.JsonResultVO;
import cn.rcth.base.VO.OrderVO;
import cn.rcth.base.domain.Order;
import cn.rcth.base.msg.ResultMsg;
import cn.rcth.base.serivce.IOrderProductSerivce;
import cn.rcth.base.serivce.IOrderSerivce;
import cn.rcth.base.util.HttpRequest;
import cn.rcth.base.util.IdGenerateUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@Controller
public class OrderController {

    @Autowired
    private IOrderSerivce orderSerivce;



    /**
     * 用户点击立即购买出现确认订单
     *  orderVO 订单 内容
     * @return 订单内容
     */
    @RequestMapping(value = "/order/Confirm",method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg orderConfirm(OrderVO orderVO){
        Map orderConfirm =  orderSerivce.orderConfirm(orderVO);
        return  ResultMsg.successResult(orderConfirm);
    }

    /**
     * 支付后生成订单(成不成功都生成)
     *  orderVO 订单 内容
     */
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg order(HttpServletRequest  request,OrderVO orderVO) throws Exception {
        //用户id 地址
        String spbill_create_ip = HttpRequest.getIpAddr(request);
        //创建订单   //发起统一支付
        ResultMsg resultMsg = orderSerivce.orderCreate(orderVO, spbill_create_ip);
        return ResultMsg.successResult(resultMsg);
    }

    /**
     * 再来一单
     * orderId 订单id
     */
    @RequestMapping(value = "/order/ComeBackOneMore", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg orderComeBackOneMore(Long orderId){
        ResultMsg resultMsg =orderSerivce.orderComeBackOneMore(orderId);
        return resultMsg;
    }

    /**
     * 确认收货
     * orderId 订单id
     */
    @RequestMapping(value = "/order/receipt", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg orderReceipt( Long orderId){
        orderSerivce.orderReceipt(orderId);
        return ResultMsg.failedResult(ResultMsg.MSG_SUCCESS_DFT);
    }


    /**
     * 查询当前用户的所有订单
     * @param userId  用户id
     * @return 订单
     */
    @RequestMapping(value = "/order/userOrder" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg userOrder(Long userId,Integer orderStatus){
        ResultMsg date = orderSerivce.getUserOrder(userId, orderStatus);
        return date;

    }

    /**
     * 查询订单详情
     * @param orderId  订单id
     * @return  订单详情
     */
    @RequestMapping(value = "/order/queryOrderDetails" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg queryOrderDetails(Long orderId){
        ResultMsg resultMsg = orderSerivce.queryOrderDetails(orderId);
        return resultMsg;

    }


    /**
     * 取消订单
     */
    @RequestMapping(value = "/cancel/order" ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResultVO cancelOrder(Long orderId){
        orderSerivce.cancelOder(orderId);
        return JsonResultVO.success();
    }

    /**
     * 查询当前用户订单数量
     * @param userId
     * @return
     */
    @RequestMapping(value = "/order/status" , method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg orderStatus(Long userId){
        Map  map = orderSerivce.orderStatus(userId);
        return ResultMsg.successResult(map);
    }

}
